<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace app\abnormalorder\controller;

class AdminOrder extends \app\AdminRest
{
    protected $model;
    protected $process_model;
    protected $info_model;
    public function __construct(\think\App $app)
    {
        parent::__construct($app);
        $this->model = new \app\abnormalorder\model\OrderList();
        $this->process_model = new \app\abnormalorder\model\OrderProcess();
        $this->info_model = new \app\abnormalorder\model\OrderInfo();
    }
    public function orderAdd()
    {
        $input = $this->_input;
        $order_model = new \app\massage\model\Order();
        $admin_id = $order_model->where(["id" => $input["order_id"]])->value("admin_id");
        $type = empty($admin_id) ? 1 : 2;
        $process_id = $this->process_model->where(["uniacid" => $this->_uniacid, "status" => 1, "type" => $type])->order("top,id desc")->value("id");
        $insert = ["uniacid" => $this->_uniacid, "order_id" => $input["order_id"], "type" => $input["type"], "bad_text" => $input["bad_text"], "customer_text" => $input["customer_text"], "deduct_cash" => $input["deduct_cash"], "process_id" => $process_id, "user_id" => $this->_user["id"]];
        $res = $this->model->dataAdd($insert);
        $order_id = $this->model->getLastInsID();
        $process = $this->process_model->where(["uniacid" => $this->_uniacid, "status" => 1, "type" => $type])->order("top,id desc")->select()->toArray();
        if(!empty($process)) {
            foreach ($process as $k => $v) {
                $insert = ["uniacid" => $this->_uniacid, "order_id" => $order_id, "process_id" => $v["id"], "top" => $k, "is_default" => 1, "sub_type" => $v["sub_type"]];
                $this->info_model->dataAdd($insert);
                if($k == 0) {
                    $info_id = $this->info_model->getLastInsID();
                    $this->model->dataUpdate(["id" => $order_id], ["info_id" => $info_id]);
                }
            }
        }
        return $this->success($order_id);
    }
    public function orderInfo()
    {
        $input = $this->_param;
        $process_model = new \app\abnormalorder\model\OrderProcess();
        $handle_model = new \app\abnormalorder\model\OrderInfoHandle();
        $wander_model = new \app\abnormalorder\model\OrderWander();
        $data = $this->model->dataInfo(["id" => $input["id"]]);
        $data = $this->model->getOrderResult($data);
        $data["deduct_time"] = $handle_model->where(["order_id" => $data["id"]])->where("status", ">", 1)->where("deduct_cash", ">", 0)->value("create_time");
        $process = $this->info_model->where(["order_id" => $data["id"]])->where("status", ">", -1)->order("top,id desc")->select()->toArray();
        if(!empty($process)) {
            foreach ($process as &$v) {
                if(1 < $v["status"]) {
                    $v["role_list"] = $handle_model->handleUserList($v["id"]);
                } else {
                    $process_info = $process_model->dataInfo(["id" => $v["process_id"]]);
                    $v["role_list"] = $process_model->getRoleData($v["process_id"], $v["pass_type"], $v["id"]);
                }
                if($data["is_deduct"] == 1) {
                    $v["deduct_status"] = 0;
                } else {
                    $v["deduct_status"] = isset($process_info["deduct_status"]) ? $process_info["deduct_status"] : 0;
                    if(!empty($v["pass_type"])) {
                        $wander = $wander_model->dataInfo(["process_id" => $v["process_id"], "pass_type" => $v["pass_type"], "status" => 1]);
                        $v["deduct_status"] = isset($wander["deduct_status"]) ? $wander["deduct_status"] : $v["deduct_status"];
                    }
                }
                if($v["status"] == 1) {
                    $v["can_handle"] = $this->model->getCanHandleUser($data, $this->_user, $v["process_id"]);
                } else {
                    $v["can_handle"] = 0;
                }
                $handel = $handle_model->where(["user_id" => $this->_user["id"], "order_info_id" => $v["id"]])->find();
                if(!empty($handel)) {
                    $v["can_handle"] = 0;
                }
            }
        }
        $arr["info"] = $data;
        $arr["process"] = $process;
        return $this->success($arr);
    }
    public function updateOrder()
    {
        $input = $this->_input;
        $insert = ["uniacid" => $this->_uniacid, "order_id" => $input["order_id"], "type" => $input["type"], "bad_text" => $input["bad_text"], "customer_text" => $input["customer_text"], "deduct_cash" => $input["deduct_cash"], "first_cancel" => 0];
        $res = $this->model->dataUpdate(["id" => $input["id"]], $insert);
        return $this->success($res);
    }
    public function handleOrder()
    {
        $input = $this->_input;
        $info = $this->info_model->dataInfo(["id" => $input["id"]]);
        if(empty($info)) {
            $this->errorMsg("参数错误");
        }
        $start = $this->info_model->where(["order_id" => $info["order_id"], "status" => 1])->order("top,id desc")->value("id");
        if(empty($start)) {
            $this->errorMsg("参数错误2");
        }
        if($info["id"] != $start) {
            $this->errorMsg("参数错误1");
        }
        $key = "abnormalorder_handle" . $input["id"];
        incCache($key, 1, $this->_uniacid);
        if(getCache($key, $this->_uniacid) != 1) {
            decCache($key, 1, $this->_uniacid);
            $this->errorMsg("当前订单正在被处理");
        }
        $deduct_cash = !empty($input["deduct_cash"]) ? $input["deduct_cash"] : 0;
        \think\facade\Db::startTrans();
        $insert = ["uniacid" => $this->_uniacid, "order_info_id" => $input["id"], "status" => $input["status"], "deduct_cash" => $deduct_cash, "text" => !empty($input["text"]) ? $input["text"] : "", "process_id" => $info["process_id"], "order_id" => $info["order_id"], "user_id" => $this->_user["id"]];
        $handle_model = new \app\abnormalorder\model\OrderInfoHandle();
        $process_model = new \app\abnormalorder\model\OrderProcess();
        $handle_model->dataAdd($insert);
        $is_next = 1;
        if($info["sub_type"] == 2 && $input["status"] == 2 && $this->_user["is_admin"] != 1) {
            $process_info = $process_model->getRoleData($info["process_id"], $info["pass_type"], $info["id"]);
            if(in_array(1, array_column($process_info, "handle_status"))) {
                $is_next = 0;
            }
        }
        if($is_next == 1) {
            $update = ["status" => $input["status"], "create_time" => time()];
            $res = $this->info_model->dataUpdate(["id" => $input["id"]], $update);
            if($update["status"] == 2) {
                $process_id = $this->info_model->where(["order_id" => $info["order_id"], "status" => 1])->where("top", ">", $info["top"])->order("top,id desc")->value("process_id");
                $info_id = $this->info_model->where(["order_id" => $info["order_id"], "status" => 1])->where("top", ">", $info["top"])->order("top,id desc")->value("id");
                $this->model->dataUpdate(["id" => $info["order_id"]], ["process_id" => $process_id, "info_id" => $info_id]);
            }
        }
        $wander_model = new \app\abnormalorder\model\OrderWander();
        if($info["pass_type"] == 0) {
            if($info["is_cancel"] == 0) {
                if($input["status"] == 3) {
                    $this->info_model->completionProcess($info);
                }
            } else {
                $pass_type = $input["status"] == 2 ? 1 : 2;
                $wander = $wander_model->dataInfo(["process_id" => $info["process_id"], "pass_type" => $pass_type, "status" => 1]);
                if(!empty($wander)) {
                    if($input["status"] == 2 && $is_next == 1 || $input["status"] == 3) {
                        $this->info_model->wanderProcess($info, $wander);
                    }
                } elseif($input["status"] == 3 && empty($wander)) {
                    $this->info_model->completionProcess($info);
                }
            }
        }
        if(isset($input["deduct_cash"]) && 0 < $input["deduct_cash"]) {
            $find = $this->model->dataInfo(["id" => $info["order_id"], "is_deduct" => 1]);
            if(!empty($find)) {
                decCache($key, 1, $this->_uniacid);
                \think\facade\Db::rollback();
                $this->errorMsg("该订单已经扣款");
            }
            $order_model = new \app\massage\model\Order();
            $order = $this->model->dataInfo(["id" => $info["order_id"]]);
            $coach_id = $order_model->where(["id" => $order["order_id"]])->value("coach_id");
            $record_model = new \app\massage\model\CashUpdateRecord();
            $res = $record_model->recordAdd($coach_id, $input["deduct_cash"], 0, $this->_user["id"], "异常订单扣款", $info["id"]);
            if(!empty($res["code"])) {
                decCache($key, 1, $this->_uniacid);
                \think\facade\Db::rollback();
                $this->errorMsg($res["msg"]);
            }
            $this->info_model->dataUpdate(["id" => $info["id"]], ["deduct_cash" => $input["deduct_cash"]]);
            $this->model->dataUpdate(["id" => $info["order_id"]], ["is_deduct" => 1]);
        }
        $next = $this->info_model->where(["order_id" => $info["order_id"], "status" => 1])->where("top", ">", $info["top"])->find();
        if(empty($next) && $is_next == 1) {
            $update = ["status" => $input["status"], "is_handle" => 1, "end_time" => time(), "end_user_id" => $this->_user["id"], "info_id" => 0];
            $this->model->dataUpdate(["id" => $info["order_id"]], $update);
        }
        \think\facade\Db::commit();
        decCache($key, 1, $this->_uniacid);
        return $this->success(true);
    }
    public function pendingOrderList()
    {
        $input = $this->_param;
        $adminRole_mdoel = new \app\node\model\RoleAdmin();
        $dis[] = ["a.uniacid", "=", $this->_uniacid];
        $dis[] = ["a.is_handle", "=", 0];
        $dis[] = ["a.first_cancel", "=", 0];
        if(!empty($input["order_code"])) {
            $dis[] = ["b.order_code", "like", "%" . $input["order_code"] . "%"];
        }
        if(!empty($input["type"])) {
            $dis[] = ["a.type", "=", $input["type"]];
        }
        if(!empty($input["start_time"])) {
            $dis[] = ["a.create_time", "between", $input["start_time"] . "," . $input["end_time"]];
        }
        $this->_user["admin_id"] = !empty($this->_user["admin_id"]) ? $this->_user["admin_id"] : $this->_user["id"];
        if($this->_user["is_admin"] == 2) {
            $role = $adminRole_mdoel->getUserRole($this->_user["id"], $this->_uniacid);
            $where = [["d.user_id", "=", $this->_user["id"]], ["d.type", "=", 2]];
            $where1 = [["d.user_id", "in", $role], ["d.type", "=", 1]];
        } elseif($this->_user["is_admin"] == 0) {
            $where = [["b.admin_id", "=", $this->_user["admin_id"]], ["d.type", "=", 3]];
        }
        if(empty($where1) && empty($where)) {
            $where = $where1 = ["a.uniacid", "=", $this->_uniacid];
        } elseif(empty($where)) {
            $where = ["a.uniacid", "=", -1];
        } elseif(empty($where1)) {
            $where1 = ["a.uniacid", "=", -1];
        }
        $user_id = $this->model->getAdminUser($this->_user);
        $user_id = implode(",", $user_id);
        $data = $this->model->alias("a")->join("massage_service_order_list b", "a.order_id = b.id")->join("massage_service_abnormal_order_role d", "d.process_id = a.process_id AND a.pass_type = d.pass_type", "left")->join("massage_service_abnormal_order_info_handle e", "a.info_id = e.order_info_id AND e.user_id in (" . $user_id . ")", "left")->where($dis)->whereNull("e.id")->where(function ($query) {
            $query->whereOr([$where, $where1]);
        })->field("a.*,b.order_code,b.coach_id,b.create_time as order_create_time,b.admin_id")->group("a.id")->order("a.id desc")->paginate($input["limit"])->toArray();
        if(!empty($data["data"])) {
            $coach_model = new \app\massage\model\Coach();
            $address_model = new \app\massage\model\OrderAddress();
            $admin_model = new \app\massage\model\Admin();
            $handle_model = new \app\abnormalorder\model\OrderInfoHandle();
            foreach ($data["data"] as &$v) {
                $v["coach_name"] = $coach_model->where(["id" => $v["coach_id"]])->value("coach_name");
                $v["user_name"] = $address_model->where(["order_id" => $v["order_id"]])->value("user_name");
                $v["admin_name"] = $admin_model->where(["id" => $v["admin_id"]])->value("agent_name");
                $v = $this->model->getOrderResult($v);
                $v["deduct_time"] = $handle_model->where(["order_id" => $v["id"]])->where("status", ">", 1)->where("deduct_cash", ">", 0)->value("create_time");
            }
        }
        $data["already_count"] = $this->model->getAlreadyOrderCount($this->_uniacid, $this->_user);
        return $this->success($data);
    }
    public function alreadyOrderList()
    {
        $input = $this->_param;
        $dis[] = ["a.uniacid", "=", $this->_uniacid];
        $dis[] = ["a.status", ">", -1];
        if(!empty($input["order_code"])) {
            $dis[] = ["b.order_code", "like", "%" . $input["order_code"] . "%"];
        }
        if(!empty($input["type"])) {
            $dis[] = ["a.type", "like", $input["type"]];
        }
        if(!empty($input["start_time"])) {
            $dis[] = ["a.create_time", "between", $input["start_time"] . "," . $input["end_time"]];
        }
        if($this->_user["is_admin"] != 1) {
            $user_id = $this->model->getAdminUser($this->_user);
            $dis[] = ["d.user_id", "in", $user_id];
        }
        if(isset($input["is_handle"])) {
            $dis[] = ["a.is_handle", "=", $input["is_handle"]];
        }
        $data = $this->model->alias("a")->join("massage_service_order_list b", "a.order_id = b.id")->join("massage_service_abnormal_order_info_handle d", "a.id = d.order_id")->where($dis)->field("a.*,b.order_code,b.coach_id,b.create_time as order_create_time,b.admin_id")->group("a.id")->order("a.id desc")->paginate($input["limit"])->toArray();
        if(!empty($data["data"])) {
            $coach_model = new \app\massage\model\Coach();
            $address_model = new \app\massage\model\OrderAddress();
            $admin_model = new \app\massage\model\Admin();
            $handle_model = new \app\abnormalorder\model\OrderInfoHandle();
            foreach ($data["data"] as &$v) {
                $v["coach_name"] = $coach_model->where(["id" => $v["coach_id"]])->value("coach_name");
                $v["user_name"] = $address_model->where(["order_id" => $v["order_id"]])->value("user_name");
                $v["admin_name"] = $admin_model->where(["id" => $v["admin_id"]])->value("agent_name");
                $v = $this->model->getOrderResult($v);
                $v["deduct_time"] = $handle_model->where(["order_id" => $v["id"]])->where("status", ">", 1)->where("deduct_cash", ">", 0)->value("create_time");
            }
        }
        $data["pending_count"] = $this->model->getPendingOrderCount($this->_uniacid, $this->_user["admin_id"], $this->_user["is_admin"]);
        return $this->success($data);
    }
}

?>